package com.lottery.htbc.job;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.lottery.htbc.pojo.BetConfig;
import com.lottery.htbc.service.JspkServiceImpl;
import com.lottery.htbc.thread.LuckDrawBase;


/**
 * 非投注任务
 */
public class HtbcTaskJob {


	private final static Logger LOGGER = LoggerFactory.getLogger(HtbcTaskJob.class);
	@Autowired
	private JspkServiceImpl jspkService;


	/**
	 * 规划 抽奖任务
	 * @throws InterruptedException 
	 */
	public void planBetTask() throws InterruptedException{
		LOGGER.info("每天凌晨一点,创建当天的投注计划");
		Random rd=new Random();
		int ycRd= rd.nextInt(9)+1;
		LOGGER.info("延迟 {} 秒钟 ",ycRd);
		long ycTimes=ycRd*1000L;
		Thread.sleep(ycTimes);
		jspkService.planBetTask();
		LOGGER.info("投注计划初始化完成");
	}

	/**
	 * 感应心跳<br/> 为了不让token失效 需要让token保持活跃
	 * @throws Exception 
	 */
	public void heartbeat() throws Exception{
		List<BetConfig>  configs=jspkService.queryValidUserConfigAll();
		if(configs!=null&&configs.size()>0){
			LuckDrawBase luckDraw=new LuckDrawBase();
			for(BetConfig config:configs){
				luckDraw.heartbeatByToken(config.getToken());
			}
		}
	}

	/**
	 * 同步上一期的中奖结果
	 * @throws Exception
	 */
	public void synProfit() throws Exception{
		LOGGER.info("\n");
		jspkService.synProfit();
	}
	/**
	 * 汇总盈利
	 * @throws Exception
	 */
	public void summaryProfit() throws Exception{
		LOGGER.info("每天23:10点通过当天的任务汇总盈利");
		jspkService.summaryProfitByTask();
		LOGGER.info("当天的盈利汇总完毕");
	}

	/**
	 * 月度数据备份
	 * @throws Exception
	 */
	public void backupsTableDataByMonth() throws Exception{
		LOGGER.info("每月的第一天的凌晨一点开始执行月度数据备份");
		jspkService.backupsTableDataByMonth();
		LOGGER.info("月度数据备份执行完毕");
	}

	/**
	 * 同步账户余额
	 * @throws Exception
	 */
	public void synBalanceValid() throws Exception{
		Calendar calendar = Calendar.getInstance();
		int hour= calendar.get(Calendar.HOUR_OF_DAY);
		if(hour<10){
			LOGGER.info("10:00之前不进行余额同步操作");
			return;
		}
		LOGGER.info("");
		LOGGER.info("同步用户余额,两个小时执行一次");
		jspkService.synBalanceValid();
	}

	/**
	 * 分析当天投将的走势
	 * @throws Exception
	 */
	public void winnerAnalysis() throws Exception{
		LOGGER.info("每天23:05点同步用户的中奖分析");
		jspkService.winnerAnalysis();
		LOGGER.info("同步用户的中奖分析完毕");
	}
	//提现json  https://player.1k998.com/api/WithdrawWalletTransaction post
	/*	{
		"draw": "2",
		"recordsFiltered": "3",
		"recordsTotal": "3",
		"data": [
			["2019-08-12 12:36:13", "W084462572344", "已支付", "1430.86", "<i class=\"text-muted\">无<\/i>"],
			["2019-07-24 11:27:14", "W519307898886", "已支付", "716.72", "<i class=\"text-muted\">无<\/i>"],
			["2019-07-19 12:43:12", "W616492542260", "已支付", "700.00", "<i class=\"text-muted\">无<\/i>"]
		],
		"header_data": ["请求时间", "取款编码", "状态", "金额", "备注"],
		"summary": [{
			"amount": "2,847.58"
		}]
	}*/


}
